﻿using System;
using System.Data.Common;
using System.Web;
using Jalal.Library.Web.Membership;
/// <summary>
/// Summary description for SessionManager
/// </summary>

namespace Jalal.Library.Web
{
    /// <summary>
    /// Site Session value handler class, should be use to access session to avoid direct access errors
    /// </summary>
    public class SessionManager
    {
        public static int ConnectionOpenCount
        {
            get
            {
                if (System.Web.HttpContext.Current.Session[Connection_Open_Count] == null)
                    System.Web.HttpContext.Current.Session[Connection_Open_Count] = 0;
                return (int)System.Web.HttpContext.Current.Session[Connection_Open_Count]; 
            }
            set { System.Web.HttpContext.Current.Session[Connection_Open_Count] = value; }
        }

        public static DbConnection Connection
        {
            get{ return (DbConnection)System.Web.HttpContext.Current.Session[DB_CONNECTION]; }
            set{ System.Web.HttpContext.Current.Session[DB_CONNECTION] = value; }
        }

        public static DbTransaction TransactionKey
        {
            get { return (DbTransaction)System.Web.HttpContext.Current.Session[Transaction_Key]; }
            set { System.Web.HttpContext.Current.Session[Transaction_Key] = value; }
        }

        // Property representing Logged in user of the site
        public static ISiteUser LoggedUser
        {
            get { return (ISiteUser)System.Web.HttpContext.Current.Session[CURRENT_USER]; }
            set { System.Web.HttpContext.Current.Session[CURRENT_USER] = value; }
        }

        // Read Only property, returns whether the user has logged-in  in the site
        public static bool IsUserLoggedIn
        {
            get { return (System.Web.HttpContext.Current.Session[CURRENT_USER] != null); }
        }

        // Property representing Logged in admin of the site
        public static ISiteUser LoggedAdminUser
        {
            get { return (ISiteUser)System.Web.HttpContext.Current.Session[CURRENT_ADMIN_USER]; }
            set { System.Web.HttpContext.Current.Session[CURRENT_ADMIN_USER] = value; }
        }

        // Read Only property, returns whether the admin has logged-in  in the site
        public static bool IsAdminUserLoggedIn
        {
            get { return (System.Web.HttpContext.Current.Session[CURRENT_ADMIN_USER] != null); }
        }

        // Property used for returning to a page requested before authentication
        // when authentication is complete
        public static string ReturnUrl
        {
            get { return (string)System.Web.HttpContext.Current.Session[RETURN_URL]; }
            set { System.Web.HttpContext.Current.Session[RETURN_URL] = value; }
        }

        // Constructors
        public SessionManager() { }

        private const string RETURN_URL = "ReturnUrl";
        private const string CURRENT_ADMIN_USER = "CurrentAdminUser";
        private const string CURRENT_USER = "CurrentUser";
        private const string DB_CONNECTION = "DbConnection";
        private const string Transaction_Key = "TransactionKey";
        private const string Connection_Open_Count = "ConnectionOpenCount";
    }
}
